package cn.MaySoon.shenfu;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;

public class handle {

    public static  boolean updatePlayerLevel(Player p) throws IOException {
        shenfu shenfu =	main.PlayerShenFuData.get(p);
        int level =shenfu.getPlayerlevel();
        if (level==100) {
            p.sendMessage("§c已达到最高等级了");
            return true;
        }
        level = level+1;

        int money =main.config.getInt("shenfu."+level+".money");

        if (main.plugin.economy.getBalance(p.getName())<money) {
            return false;
        }
        else {
            main.plugin.economy.withdrawPlayer(p.getName(), money);
            if (shenfu.getPlayerxing()==1) {

                Bukkit.broadcastMessage("§f玩家§a§n"+p.getName()+"§r §f将神符成功升至§a"+shenfu.getPlayerjie()+"§f阶,获得强力刷BOSS效果!");
            }

            shenfu.setPlayerlevel(level);

            setPlayerData(p);
            AP.updatePlayerAttribute(p);
            return true;
        }
    }
    public static shenfu getPlayerData(Player p) {
        File file = new File(main.plugin.getDataFolder()+ "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        shenfu shenfu = new shenfu();
        shenfu.setPlayerlevel(data.getInt("level"));
        return shenfu;
    }

    public static   void setPlayerData(Player p) throws IOException {
        File file = new File(main.plugin.getDataFolder()+ "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        shenfu shenfu = 	main.PlayerShenFuData.get(p);
        data.set("level",shenfu.getPlayerlevel());
        data.save(file);
    }

}
